<?php
namespace app\admin\logic;
use app\admin\validate\ClassKu as ClassKuValidate;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;

class ClassKuLogic extends BaseLogic {

    /**
     * 获取题库查询条件
     * @param $name
     * @return array
     */
    public function get_search_ck_maps($name){
        $maps = [];
        if(!empty($name)){
            $maps[] = ['name','like','%' . $name . '%'];
        }
        return $maps;
    }

    /**
     * 检查新增题库规则
     * @param $params
     * @return mixed
     */
    public function check_add_ck_rule($params){
        $validate = new ClassKuValidate();
        $r = $validate->scene('add')->check($params);
        if(!$r){
            return $this->get_response(0,$validate->getError());
        }
        return $this->get_response(1,'OK');
    }

    /**
     * 新增题库
     * @param $params
     * @return mixed
     */
    public function add_ck($params){
        $ck_data = [];
        $ck_data['name'] = trim($params['name']);
        $ck_data['remark'] = trim($params['remark']);
        $r = model('class_ku')->insertGetId($ck_data);
        if($r){
            $data['ckid'] = $r;
            return $this->get_response(1,'OK',$data);
        }
        return $this->get_response(0,'新增失败');
    }

    /**
     * 导入题
     * @param $ckid
     * @param $file
     * @return mixed
     */
    public function import_ti($ckid,$file){
        if(!empty($file)){
            $info = $file->move(\think\facade\App::getRootPath() . 'public/uploads');
            $filename = 'uploads/' . $info->getSaveName();
        }else{
            return $this->get_response(0,'请上传文件');
        }
        if($filename){
            $pathinfo = pathinfo($filename);

            if($pathinfo['extension'] != 'xlsx'){
                return $this->get_response(0,'文件为excel表格，且必须为xlsx格式');
            }

            $encode = 'utf-8';
            $objReader = IOFactory::createReader('Xlsx');
            $objReader->setReadDataOnly(true);
            $objPHPExcel = $objReader->load($filename,$encode);
            $objWorksheet = $objPHPExcel->getActiveSheet(0);//获取第一个工作表
            $highestRow = $objWorksheet->getHighestRow();//取得总行数
            $highestColumn = $objWorksheet->getHighestColumn();//取得总列数
            $highestColumnIndex = Coordinate::columnIndexFromString($highestColumn);
            $excelData = array();
            for($row = 2; $row <= $highestRow; $row++) {//如果是第2行导入 需改$row = 2
                $excelRow = $row - 2;
                for ($col = 1; $col < $highestColumnIndex + 1; $col++) {
                    $excelData[$excelRow][] = (string)$objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
                }
            }
            //halt($excelData);
            $dataMysql = [];
            foreach($excelData as $k => $v){
                $option = [];
                $question = trim($v[1]);

                $a = trim($v[2]);
                if($a != ''){
                    $option['A'] = $a;
                }
                $b = trim($v[3]);
                if($b != ''){
                    $option['B'] = $b;
                }
                $c = trim($v[4]);
                if($c != ''){
                    $option['C'] = $c;
                }
                $d = trim($v[5]);
                if($d != ''){
                    $option['D'] = $d;
                }
                $e = trim($v[6]);
                if($e != ''){
                    $option['E'] = $e;
                }
                $answer = trim($v[7]);
                //$titile = trim($v[8]);
                $cailiao = trim($v[8]);

                $temp_data['ckid'] = $ckid;
                $temp_data['question'] = $question;
                $temp_data['option'] = json_encode($option,JSON_UNESCAPED_UNICODE);
                $temp_data['answer'] = $answer;
                //$temp_data['titile'] = $titile;
                $temp_data['cailiao'] = $cailiao;
                $dataMysql[] = $temp_data;
            }

            $r = model('class_ti')->insertAll($dataMysql);
            if($r){
                $data['total_num'] = count($excelData);
                $data['success_num'] = count($dataMysql);
                return $this->get_response(1,'OK',$data);
            }
            return $this->get_response(0,'导入失败');
        }
    }

    /**
     * 检查修改题库规则
     * @param $params
     * @return mixed
     */
    public function check_save_ck_rule($params){
        $validate = new ClassKuValidate();
        $r = $validate->scene('edit')->check($params);
        if(!$r){
            return $this->get_response(0,$validate->getError());
        }

        $maps = [];
        $maps[] = ['id','=',$params['ckid']];
        $ck_detail = model('class_ku')->get_detail($maps,'id');
        if(empty($ck_detail)){
            return $this->get_response(0,'该题库不存在');
        }

        $data['ck_detail'] = $ck_detail;
        return $this->get_response(1,'OK',$data);
    }

    /**
     * 检查删除题库规则
     * @param $params
     * @return mixed
     */
    public function check_del_ck_rule($params){
        $ckid_arr = model('class_learn')->get_column([],'ckid');
        foreach($ckid_arr as $k => $v){
            $temp_arr = !empty($v) ? json_decode($v,true) : [];
            if(key_exists($params['ckid'],$temp_arr)){
                return $this->get_response(0,'该题库已被使用，不能删除');
            }
        }
        return $this->get_response(1,'OK');
    }

    /**
     * 获取公用题库查询条件
     * @return array
     */
    public function get_search_public_ck_maps(){
        $maps = [];
        return $maps;
    }
}